Ext.define('component.analyze.month', {
	extend : 'Ext.form.Panel',
	initComponent:function(){
		//电话来源
		var terminalModelStoreList = [
            {"value":"0", "text":"客户"}
        ];
	    var terminalModelStore = Ext.data.Store({
	    	autoDestroy: true,
	        fields :["value","text"],
	        data : terminalModelStoreList
	    });
	    
	    //时间段
		var timeRangeStoreList = [
            {"value":"1", "text":"自定义"},
            {"value":"2", "text":"全部"}
        ];
	    var timeRangeStore = Ext.data.Store({
	    	autoDestroy: true,
	        fields :["value","text"],
	        data : timeRangeStoreList
	    });
	    
	    Ext.define('department', {
	        extend: 'Ext.data.Model',
	        fields: [
	            {name: 'id', type: 'string'},
	            {name: 'departmentCode', type: 'string'},
	            {name: 'permissionCode', type: 'string'},
	            {name: 'departmentName',  type: 'string'},
	        ]
	    });
	    
	    var departmentStore = Ext.create("Ext.data.Store", {
	    	model: 'department',
	        proxy: {
	            type: 'ajax',
	            url: jypath+'/system/user/findAllDept',
	            reader: {
	                type: 'json',
	                root: 'obj'
	            }
	        },
	        autoLoad: true
	    });
	    
	    var userStore = new Ext.data.Store({
	        fields: ["userName", "id"],
	        proxy:{
	            type:'ajax',
	            url:jypath+'/system/user/findUserByDeptPath',
	            reader:{
	                type: 'json', 
	                root: 'root'  //数据
	           }
	        },
            autoLoad: false
	    });
		var formMonth = Ext.create("Ext.form.Panel", {
			minHeight:120,
		    title: "查询条件",
		    collapsible: true,  //可折叠
		    autoScroll: false,   //自动创建滚动条
		    defaultType: 'textfield',
		    fieldDefaults: {
		        margin: 2,
		        labelWidth: 60,
		        labelAlign: "right",
		        margin: "5 0 0 0"
		    },
		    items: [
				{
					xtype : "container",
					layout : "hbox",
					items : [{
						xtype : 'combobox',
						fieldLabel : '来源',
						store:terminalModelStore,
				    	displayField: "text",
				    	valueField:"value",
				    	id:'terminalModel_month',
				    	name:'terminalModel',
				    	width: 200,
				    	listeners:{
			                afterRender : function() {
			                	Ext.getCmp('terminalModel_month').setValue('0');
				            }
			            }
					},{
						xtype : 'combobox',
						fieldLabel : '员工',
						store:departmentStore,
				    	displayField: "departmentName",
				    	valueField:"permissionCode",
				    	id:'departmentCode_month',
				    	name:'departmentCode',
				    	queryMode : 'local',
				    	width: 200,
				    	listeners:{
			            	change:function(value){
			            		Ext.getCmp('createUserId_month').setValue("");
			            		userStore.load({
			            			params:{
			            				'deptPath': value.lastValue
			            			}
				            	});
			            	}
			            }
					},{
						xtype: "displayfield", value: "&nbsp;"
					},{
						xtype : 'combobox',
						store:userStore,
				    	displayField: "userName",
				    	valueField:"id",
				    	id:'createUserId_month',
				    	name:'createUserId',
				    	queryMode : 'local',
				    	width: 120
					},{
						xtype: "datefield", id: "startTimeAfterMonthStr", fieldLabel : '开始时间', name: "startTimeAfterStr", format:'Y-m', width: 200
					},{ 
						xtype: "checkboxgroup",
						id:"callCheckType_month",
						name :"callCheck",
						width:220,
						items:[{
					    		boxLabel:'总数',
					    		inputValue:'0'
					    	},{
					    		boxLabel:'<label style="color: green;">拨通数</label>',
					    		inputValue:'1'
					    	},{
					    		boxLabel:'未拨通数',
					    		inputValue:'2'
					    	}
					     ]
					}]
				}
            ],
		    buttons: [
  	        	{ xtype: "button", text: "查询", 
	  	        	handler:function(){
	  	        		var callCheckType = Ext.getCmp('callCheckType_month').getChecked();
	  	        		var ckStatus="";
	  	        		Ext.Array.each(callCheckType, function(item){
	  	        			ckStatus += item.inputValue+',';
	  	        		});
	  	        		gridStoreMonth.getProxy().extraParams.callCheck = ckStatus;
	  	        		gridStoreMonth.load();
	  	        		var pagingTool=Ext.getCmp("pagingTool");
	  	        		pagingTool.moveFirst();//跳至第一页
  	        		}
  	        	},
	  	        { xtype: "button", text: "清空",
	  	        	handler:function(){
	  	        		formMonth.getForm().reset();
	  	        	}
	  	        }
        	]    
		});
	
		var gridStoreMonth = Ext.create('Ext.data.Store', {
	        fields: ["createUserId","userName","departmentName","monthOne","monthTwo","monthThree","monthFour","monthFive",
	                 "monthSix","monthSeven","monthEight","monthNine","monthTen","monthEleven","monthTwelve",
                 	 "monthOneTime","monthTwoTime","monthThreeTime","monthFourTime","monthFiveTime","monthSixTime","monthSevenTime",
	                 "monthEightTime","monthNineTime","monthTenTime","monthElevenTime","monthTwelveTime"],
	        pageSize: 10,  //页容量15条数据
	        //是否在服务端排序 （true的话，在客户端就不能排序）
	        remoteSort: false,
	        proxy: {
	            type: 'ajax',
	            url: jypath+'/system/telRecordAnalyze/analyze_month_list',
	            actionMethods: { read: 'POST' },
	            reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
	                type: 'json', //返回数据类型为json格式
	                root: 'rows',  //数据
	                totalProperty: 'total' //数据总条数
	            }
	        },
	        autoLoad: true  //即时加载数据
	    });
	
		gridStoreMonth.on("beforeload",function(){
	        Ext.apply(gridStoreMonth.proxy.extraParams,formMonth.getForm().getValues());
	    });
	
		var gridMonth = Ext.create('Ext.grid.Panel', {
			minHeight:600,
	        store: gridStoreMonth,
	        columnLines: true,
	        columns: [{
	            text: '部门',
	            dataIndex: 'departmentName'
	        },{
	            text     : '员工', 
	            dataIndex: 'userName'
	        },{
	            text     : 'monthOne',
	            dataIndex: 'monthOne',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	            	return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthOneTime");
	            }
	        },{
	            text     : 'monthTwo',
	            dataIndex: 'monthTwo',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	            	return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthTwoTime");
	            }
	        },{
	            text     : 'monthThree',
	            dataIndex: 'monthThree',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	            	return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthThreeTime");
	            }
	        },{
	            text     : 'monthFour',
	            dataIndex: 'monthFour',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	            	return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthFourTime");
	            }
	        },{
	            text     : 'monthFive',
	            dataIndex: 'monthFive',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	            	return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthFiveTime");
	            }
	        },{
	            text     : 'monthSix',
	            dataIndex: 'monthSix',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	            	return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthSixTime");
	            }
	        },{
	            text     : 'monthSeven',
	            dataIndex: 'monthSeven',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	                return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthSevenTime");
	            }
	        },{
	            text     : 'monthEight',
	            dataIndex: 'monthEight',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	                return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthEightTime");
	            }
	        },{
	            text     : 'monthNine',
	            dataIndex: 'monthNine',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	                return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthNineTime");
	            }
	        },{
	            text     : 'monthTen',
	            dataIndex: 'monthTen',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	                return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthTenTime");
	            }
	        },{
	            text     : 'monthEleven',
	            dataIndex: 'monthEleven',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	                return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthElevenTime");
	            }
	        },{
	            text     : 'monthTwelve',
	            dataIndex: 'monthTwelve',
	            renderer : function(val,cellmeta,record,rowIndex,columnIndex,store) {
	                return rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,"monthTwelveTime");
	            }
	        }],
	        title: '月报表',
	        dockedItems: [{  
	            xtype: 'pagingtoolbar',  
	            store: gridStoreMonth,   // GridPanel中使用的数据  
	            dock: 'bottom',  
	            displayInfo: true,
	            emptyMsg:"没有数据",
	            displayMsg:"显示从{0}条数据到{1}条数据，共{2}条数据",
	            id:'pagingTool'
	        }]
	    });
	
		
		var defaultConfigg = {
			defaults: {  
	            split: true,    //有分割线  
	            collapsible: true,  //可以折叠
	        },
			items:[formMonth,gridMonth]
		};
		Ext.apply(this,defaultConfigg);
		this.callParent(arguments);
		
	}
});

function rendererMonthColumn(val,cellmeta,record,rowIndex,columnIndex,store,gridMonth,columnText){
	var valArr = val.split("|");
	var returnStr = "";
	for(var ii=0; ii<valArr.length; ii++){
		if(ii==1){
			returnStr += "<label style='color: green; min-width:15px;'>" + valArr[ii] + "</label>";
		}else{
			returnStr += "<label style='min-width:15px;'>" + valArr[ii] + "</label>";
		}
		if((valArr.length - 1) != ii){
			returnStr += "&emsp;"
		}
	}
	//动态改变Column的显示标题
	gridMonth.columns[columnIndex].setText(record.get(columnText));
	return returnStr;
}
